// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;
using System.Collections.ObjectModel;

namespace Tierless.Framework.Entities
{
	/// <summary>
	/// Represents an observable collection of <see cref="EntityColumn"/> instances.
	/// </summary>
	public class EntityColumnCollection : ObservableCollection<EntityColumn>
	{
		/// <summary>
		/// Gets the column with the given name, if it exists. Otherwise, throws an exception.
		/// </summary>
		/// <param name="columnName">The name of the column.</param>
		/// <exception cref="ArgumentException"/>
		/// <returns>The column with the given name.</returns>
		public EntityColumn this[string columnName]
		{
			get
			{
				var columnIndex = IndexOf(columnName);
				if (columnIndex < 0)
					throw new ArgumentException(String.Format("Column '{0}' not found.", columnName));
				return this[columnIndex];
			}
		}

		/// <summary>
		/// Returns the index of the column with the given name, if it exists, -1 otherwise.
		/// </summary>
		/// <param name="columnName">The name of the column.</param>
		/// <returns>The index of the column with the given name.</returns>
		public int IndexOf(string columnName)
		{
			for (var index = 0; index < Count; index++)
				if (this[index].Name == columnName)
					return index;

			return -1;
		}

		/// <summary>
		/// Returns true if the collection contains a column with the given name, false otherwise.
		/// </summary>
		/// <param name="columnName">The name of the column.</param>
		/// <returns>True if the collection contains a column with the given name, false otherwise.</returns>
		public bool Contains(string columnName)
		{
			return IndexOf(columnName) >= 0;
		}
	}
}